<template>
  <el-main width="75%" style="background-color: rgb(238, 241, 246)" id="main">
    <Project/>
    <Entity/>
    <Mapper v-show="projectSetting.ormType!==3"/>
    <Repository v-show="projectSetting.ormType===3"/>
    <Service/>
    <Controller/>
    <el-row :gutter="20">
      <el-col :span="10" :offset="7">
        <el-button type="primary" @click="saveSettingModalShow" :disabled="!linkIsOpen">保存配置</el-button>
        <el-button type="primary" @click="openCodeFolder" :disabled="!projectSetting.codePath">打开目录</el-button>
        <el-button type="primary" @click="generateCode" :disabled="!linkIsOpen">生成代码</el-button>
      </el-col>
      <el-col :span="3" :offset="4">
        <a class="info" @click="infoModalShow">软件信息</a>
      </el-col>
    </el-row>
    <MappingModal/>
    <ChooseSettingModal/>
    <EditLinkModal/>
    <DeleteLinkModal/>
    <CreateLinkModal/>
    <SaveSettingModal/>
    <SingleTableMappingModal/>
    <InfoModal/>
  </el-main>
</template>

<script>
  import Project from "./settings/Project";
  import Entity from "./settings/Entity";
  import Mapper from "./settings/Mapper";
  import Repository from "./settings/Repository";
  import Service from "./settings/Service";
  import Controller from "./settings/Controller";
  import MappingModal from "./modals/MappingModal";
  import ChooseSettingModal from "./modals/ChooseSettingModal";
  import EditLinkModal from "./modals/EditLinkModal";
  import DeleteLinkModal from "./modals/DeleteLinkModal";
  import CreateLinkModal from "./modals/CreateLinkModal";
  import SaveSettingModal from "./modals/SaveSettingModal";
  import InfoModal from "./modals/InfoModal";
  import SingleTableMappingModal from "./modals/SingleTableMappingModal";
  import Pubsub from 'pubsub-js';
  import {mapState} from "vuex";

  export default {
    name: "ContentPanel",
    components: {
      Project, Entity, Mapper,Repository, Service, Controller,
      MappingModal, ChooseSettingModal, EditLinkModal,
      DeleteLinkModal, CreateLinkModal, SaveSettingModal,
      SingleTableMappingModal,InfoModal
    },
    computed: {
      ...mapState(["linkIsOpen", "currentLinkInfo",
        "projectSetting", "entitySetting",
        "mapperSetting","repositorySetting", "serviceSetting",
        "controllerSetting"])
    },
    methods: {
      infoModalShow() {
        this.$store.dispatch("infoModalShow", true);
      },
      saveSettingModalShow() {
        this.$store.dispatch("saveSettingModalShow", true);
      },
      generateCode() {
        let databases = getDatabases(this);

        if (databases.length === 0) {
          this.$message({
            message: '请打开连接后,选择要生成代码的表!',
            type: 'warning'
          });
          return;
        }

        if (generateCheck(this)) {
          Pubsub.publish("generating", true);
          setTimeout(() => {
            this.$store.dispatch("generateCode", databases);
          }, 1000)
        }
      },
      openCodeFolder(){
        this.$store.dispatch("openCodeFolder");
      }
    }
  }

  function getDatabases(vm) {
    let currentLinkInfoDatabases = vm.currentLinkInfo.databases;
    let databases = [];
    currentLinkInfoDatabases.forEach(database => {
      let tables = [];
      database.tables.forEach(table => {
        if (table.checked) {
          tables.push({table: table.name});
        }
      });
      if (tables.length > 0) {
        let db = {
          database: database.name,
          tables: tables
        };
        databases.push(db);
      }
    });

    return databases;
  }

  function generateCheck(vm) {
    if ("{}" === JSON.stringify(vm.currentLinkInfo)) {
      vm.$message({
        message: '请打开连接后,选择要生成代码的表!',
        type: 'warning'
      });
      return false;
    }
    if (!vm.projectSetting.projectPackage) {
      vm.$message({
        message: '请输入项目包名!',
        type: 'warning'
      });
      return false;
    }
    if (!vm.projectSetting.codePath) {
      vm.$message({
        message: '请输入代码保存路径!',
        type: 'warning'
      });
      return false;
    }
    if (!vm.entitySetting.entityPackage) {
      vm.$message({
        message: '请输entity包名!',
        type: 'warning'
      });
      return false;
    }
    if (!vm.mapperSetting.mapperPackage) {
      vm.$message({
        message: '请输mapper包名!',
        type: 'warning'
      });
      return false;
    }
    if (vm.projectSetting.createService && !vm.serviceSetting.servicePackage) {
      vm.$message({
        message: '请输service包名!',
        type: 'warning'
      });
      return false;
    }
    if (vm.projectSetting.createController && !vm.controllerSetting.controllerPackage) {
      vm.$message({
        message: '请输controller包名!',
        type: 'warning'
      });
      return false;
    }
    return true;
  }
</script>

<style scoped>
  .info{
    text-decoration: underline;
  }
  .info:hover {
    color: blue;
    cursor:pointer;
  }
</style>
